package com.example.elmmerchants.Create_Shop;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import com.example.elmmerchants.R;
import com.google.firebase.firestore.FirebaseFirestore;

public class Create_Shop_Page extends AppCompatActivity {

    RelativeLayout main_category_frame_layout, second_main_category_frame_layout;
    LinearLayout choose_main, choose_second, category_item, detail_item, detail_third_layout, second_category_item, second_detail_item, second_detail_third_layout;
    String[] category = {"美食", "饮品", "甜点", "超市便利", "鲜花绿植", "日用百货", "数码家电", "水果", "宠物", "食材", "酒水茶饮", "休闲食品", "母婴玩具", "美妆日化", "服饰鞋帽", "医药健康"};
    String[][] second_choose = {{"快餐简餐", "小吃", "中式正餐", "全球美食", "火锅"},
            {"咖啡", "纯茶/凉茶", "奶茶", "果汁", "其他饮品"},
            {"西式点心", "生日蛋糕", "中式糕点", "冰凉甜点", "水果捞", "其他甜点"},
            {"小型超市", "便利店"},
            {"鲜花店", "综合鲜花绿植店", "绿植/园艺店"},
            {"五金日用店", "文具店", "书店", "餐具厨具店", "家居饰品店", "节庆用品/礼品店", "综合日用百货店"},
            {"手机通讯品牌店", "电脑数码品牌店", "家用电器品牌店", "3C电器综合店", "运营商店"},
            {"整果店", "果切店", "果捞店"},
            {"宠物食品/用品店"},
            {"菜市场", "综合生鲜果蔬超市", "火锅专营店", "肉禽店", "海鲜/水产品", "烧烤专营店", "预制菜专营店"},
            {"酒水店", "水站", "奶站", "茶行"},
            {"零食店", "干果炒货店", "冰品店", "地方特产店", "粮油调味店"},
            {"婴童服饰鞋帽店", "玩具/潮玩/模型店", "综合母婴店"},
            {"综合美妆日化专营店", "美妆品牌专卖店"},
            {"体育用品店", "运动鞋/服店", "男装店", "女装店", "男女鞋店", "内衣/家居服店", "服饰配件/皮帶/帽子/围巾店", "综合服饰鞋帽店", "珠宝首饰店"},
            {"综合药店", "眼镜店", "成人/情趣用品店", "营养保健品店", "医疗器械店", "中药店"}};
    String[][] third_choose = {{"意面披萨", "汉堡", "牛排", "三明治", "轻食/沙拉/健康餐", "焗饭", "咖喱饭", "日式便当/日本寿司", "石锅拌饭/铁板饭/板烧饭", "紫菜包饭", "烤肉拌饭", "麻辣烫/关东煮", "蛋包饭", "冒菜", "小碗菜/小份菜", "盖浇饭", "黄焖鸡米饭", "煲仔饭", "鸡公煲", "沙县小吃", "木桶饭", "猪脚饭", "排骨米饭", "蒸鸡/滋补鸡", "酸菜鱼/水煮鱼", "炒饭", "其他饭类套餐", "饺子", "其他粥店", "锅贴/煎饺", "馄饨", "包子/生煎", "煎饼果子/卷饼/手抓饼", "烧饼/馅饼", "锅盔", "肉夹馍", "驴肉火烧", "凉皮/凉粉/擀面皮", "肠粉", "羊汤/牛肉汤", "鸭血粉丝汤", "泡馍", "胡辣汤", "砂锅", "瓦罐", "豆浆/油条", "米粉/米线", "花甲粉", "螺蛳粉", "其他汤粉店", "热干面", "日式拉面", "中式拉面", "刀削面", "重庆小面", "炸酱面", "焖面", "烩面", "炒面/炒饼", "其他面馆", "跷脚牛肉", "大盘鸡", "砂锅粥"},
            {"炸鸡及炸物类小吃", "卤味类小吃", "铁板类小吃", "炒货类小吃", "冷串类小吃", "地方小吃", "炸串小吃"},
            {"北京菜", "本帮江浙菜", "川菜", "湘菜", "云南菜", "贵州菜", "西北菜", "东北菜", "湖北菜", "台湾菜", "粤菜", "港菜/茶餐厅", "新疆菜", "鲁菜", "闽菜", "江西菜", "徽菜", "其他地方菜", "海鲜", "小龙虾", "创意/概念菜", "素食", "清真菜", "农家菜/家常菜", "焖锅", "烤鱼", "麻辣香锅", "干锅", "烧烤"},
            {"新加坡菜", "泰国菜", "印度菜", "越南菜", "马来西亚菜", "其他东南亚菜", "法国菜", "巴西菜", "意大利菜", "中亚/中东菜", "俄罗斯菜", "西班牙菜", "其他西餐", "墨西哥菜", "日本料理", "韩国料理"},
            {"串串香", "川渝火锅", "云贵火锅", "老北京火锅", "港式火锅/打边炉", "鱼火锅", "羊蝎子火锅", "小火锅", "其他火锅", "潮汕火锅", "蛙类火锅"}};
    CardView last_cardview, second_last_cardview;
    TextView last_text_view, last_detail_text_view, last_detail_third_text_view, main_category, ancillary_product, second_last_text_view, second_last_detail_text_view, second_last_detail_third_text_view;
    Button main_category_well_chosen, second_main_category_well_chosen;
    View main_category_view, ancillary_product_view;
    String first_category = "暂无";
    String second_category = "暂无";
    String user_email;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_shop_page);

        user_email = getIntent().getStringExtra("user_email");
        main_category_frame_layout = findViewById(R.id.main_category_frame_layout);
        second_main_category_frame_layout = findViewById(R.id.second_main_category_frame_layout);
        category_item = findViewById(R.id.category_item);
        detail_item = findViewById(R.id.detail_item);
        detail_third_layout = findViewById(R.id.detail_third_layout);
        main_category_well_chosen = findViewById(R.id.main_category_well_chosen);
        main_category = findViewById(R.id.main_category);
        ancillary_product = findViewById(R.id.ancillary_product);
        main_category_view = findViewById(R.id.main_category_view);
        ancillary_product_view = findViewById(R.id.ancillary_product_view);

        choose_main = findViewById(R.id.choose_main);
        choose_second = findViewById(R.id.choose_second);

        first_page();

        choose_main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                main_category_frame_layout.setVisibility(View.VISIBLE);
                second_main_category_frame_layout.setVisibility(View.GONE);
                main_category.setTextColor(Color.BLACK);
                main_category_view.setBackgroundColor(Color.YELLOW);
                ancillary_product_view.setBackgroundColor(Color.WHITE);
                ancillary_product.setTextColor(Color.rgb(128, 128, 128));
            }
        });
    }

    public void first_page() {
        for (int i = 0; i < category.length; i++) {
            main_category_well_chosen.setEnabled(false);
            View item_view = LayoutInflater.from(this).inflate(R.layout.category_item_view, null);
            CardView item = item_view.findViewById(R.id.item);
            item.setBackgroundColor(Color.rgb(245, 246,251));
            item.setPadding(0, 25, 0, 25);
            TextView item_name = item_view.findViewById(R.id.item_name);
            item_name.setText(category[i]);
            if (i == 0) {
                item.setBackgroundColor(Color.WHITE);
                last_cardview = item;
                item_name.setTextColor(Color.rgb(224, 143, 87));
                last_text_view = item_name;
                detail_item.removeAllViews();
                for (int j = 0; j < second_choose[i].length; j++) {
                    View detail_view = LayoutInflater.from(this).inflate(R.layout.detail_item_view, null);
                    CardView detail_card_view = detail_view.findViewById(R.id.item);
                    detail_card_view.setPadding(0, 25, 0, 25);
                    TextView detail_item_name = detail_view.findViewById(R.id.item_name);
                    detail_item_name.setText(second_choose[i][j]);
                    int finalJ = j;
                    int finalI1 = i;
                    detail_card_view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            main_category_well_chosen.setEnabled(false);
                            if (last_detail_text_view != null) {
                                last_detail_text_view.setTextColor(Color.BLACK);
                            }
                            last_detail_text_view = detail_item_name;
                            detail_item_name.setTextColor(Color.rgb(224, 143, 87));
                            detail_third_layout.removeAllViews();
                            for (int k = 0; k < third_choose[finalJ].length; k++) {
                                View detail_third_view = LayoutInflater.from(view.getContext()).inflate(R.layout.detail_third_item_view, null);
                                CardView detail_third_card_view = detail_third_view.findViewById(R.id.item);
                                detail_third_card_view.setPadding(0, 25, 0, 25);
                                TextView detail_third_item_name = detail_third_view.findViewById(R.id.item_name);
                                detail_third_item_name.setText(third_choose[finalJ][k]);
                                int finalK = k;
                                detail_third_card_view.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        if (last_detail_third_text_view != null) {
                                            last_detail_third_text_view.setTextColor(Color.BLACK);
                                        }
                                        last_detail_third_text_view = detail_third_item_name;
                                        detail_third_item_name.setTextColor(Color.rgb(224, 143, 87));
                                        main_category_well_chosen.setEnabled(true);
                                        main_category_well_chosen.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                first_category = category[finalI1] + "/" + second_choose[finalI1][finalJ] + "/" + third_choose[finalJ][finalK];
                                                second_page();
                                            }
                                        });

                                    }
                                });
                                detail_third_layout.addView(detail_third_view);
                            }
                        }
                    });
                    detail_item.addView(detail_view);
                }
            }
            int finalI = i;
            item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    main_category_well_chosen.setEnabled(false);
                    last_cardview.setBackgroundColor(Color.rgb(245, 246,251));
                    last_text_view.setTextColor(Color.BLACK);
                    item.setBackgroundColor(Color.WHITE);
                    last_cardview = item;
                    item_name.setTextColor(Color.rgb(224, 143, 87));
                    last_text_view = item_name;
                    detail_item.removeAllViews();
                    detail_third_layout.removeAllViews();
                    for (int j = 0; j < second_choose[finalI].length; j++) {
                        View detail_view = LayoutInflater.from(view.getContext()).inflate(R.layout.detail_item_view, null);
                        CardView detail_card_view = detail_view.findViewById(R.id.item);
                        detail_card_view.setPadding(0, 25, 0, 25);
                        TextView detail_item_name = detail_view.findViewById(R.id.item_name);
                        detail_item_name.setText(second_choose[finalI][j]);
                        int finalJ = j;
                        detail_card_view.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (last_detail_text_view != null) {
                                    last_detail_text_view.setTextColor(Color.BLACK);
                                }
                                detail_item_name.setTextColor(Color.rgb(224, 143, 87));
                                last_detail_text_view = detail_item_name;
                                if (finalI == 0) {
                                    main_category_well_chosen.setEnabled(false);
                                    detail_third_layout.removeAllViews();
                                    for (int k = 0; k < third_choose[finalJ].length; k++) {
                                        View detail_third_view = LayoutInflater.from(view.getContext()).inflate(R.layout.detail_third_item_view, null);
                                        CardView detail_third_card_view = detail_third_view.findViewById(R.id.item);
                                        detail_third_card_view.setPadding(0, 25, 0, 25);
                                        TextView detail_third_item_name = detail_third_view.findViewById(R.id.item_name);
                                        detail_third_item_name.setText(third_choose[finalJ][k]);
                                        int finalK = k;
                                        detail_third_card_view.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                if (last_detail_third_text_view != null) {
                                                    last_detail_third_text_view.setTextColor(Color.BLACK);
                                                }
                                                last_detail_third_text_view = detail_third_item_name;
                                                detail_third_item_name.setTextColor(Color.rgb(224, 143, 87));
                                                main_category_well_chosen.setEnabled(true);
                                                main_category_well_chosen.setOnClickListener(new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View view) {
                                                        first_category = category[finalI] + "/" + second_choose[finalI][finalJ] + "/" + third_choose[finalJ][finalK];
                                                        second_page();
                                                    }
                                                });
                                            }
                                        });
                                        detail_third_layout.addView(detail_third_view);
                                    }
                                }
                                else {
                                    main_category_well_chosen.setEnabled(true);
                                    main_category_well_chosen.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            first_category = category[finalI] + "/" + second_choose[finalI][finalJ];
                                            second_page();
                                        }
                                    });
                                }
                            }
                        });
                        detail_item.addView(detail_view);
                    }
                }
            });
            category_item.addView(item_view);
        }
    }

    public void second_page() {
        main_category_frame_layout.setVisibility(View.GONE);
        second_main_category_frame_layout.setVisibility(View.VISIBLE);
        ancillary_product.setTextColor(Color.BLACK);
        ancillary_product_view.setBackgroundColor(Color.YELLOW);
        main_category_view.setBackgroundColor(Color.WHITE);
        main_category.setTextColor(Color.rgb(128, 128, 128));

        second_category_item = findViewById(R.id.second_category_item);
        second_detail_item = findViewById(R.id.second_detail_item);
        second_detail_third_layout = findViewById(R.id.second_detail_third_layout);
        second_main_category_well_chosen = findViewById(R.id.second_main_category_well_chosen);
        second_category_item.removeAllViews();
        second_detail_item.removeAllViews();
        second_detail_third_layout.removeAllViews();

        for (int i = 0; i < category.length; i++) {
            View item_view = LayoutInflater.from(this).inflate(R.layout.category_item_view, null);
            CardView item = item_view.findViewById(R.id.item);
            item.setBackgroundColor(Color.rgb(245, 246,251));
            item.setPadding(0, 25, 0, 25);
            TextView item_name = item_view.findViewById(R.id.item_name);
            item_name.setText(category[i]);
            if (i == 0) {
                item.setBackgroundColor(Color.WHITE);
                second_last_cardview = item;
                item_name.setTextColor(Color.rgb(224, 143, 87));
                second_last_text_view = item_name;
                second_detail_item.removeAllViews();
                for (int j = 0; j < second_choose[i].length; j++) {
                    View detail_view = LayoutInflater.from(this).inflate(R.layout.detail_item_view, null);
                    CardView detail_card_view = detail_view.findViewById(R.id.item);
                    detail_card_view.setPadding(0, 25, 0, 25);
                    TextView detail_item_name = detail_view.findViewById(R.id.item_name);
                    detail_item_name.setText(second_choose[i][j]);
                    int finalJ = j;
                    int finalI1 = i;
                    detail_card_view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (second_last_detail_text_view != null) {
                                second_last_detail_text_view.setTextColor(Color.BLACK);
                            }
                            second_last_detail_text_view = detail_item_name;
                            detail_item_name.setTextColor(Color.rgb(224, 143, 87));
                            second_detail_third_layout.removeAllViews();
                            for (int k = 0; k < third_choose[finalJ].length; k++) {
                                View detail_third_view = LayoutInflater.from(view.getContext()).inflate(R.layout.detail_third_item_view, null);
                                CardView detail_third_card_view = detail_third_view.findViewById(R.id.item);
                                detail_third_card_view.setPadding(0, 25, 0, 25);
                                TextView detail_third_item_name = detail_third_view.findViewById(R.id.item_name);
                                detail_third_item_name.setText(third_choose[finalJ][k]);
                                int finalK = k;
                                String this_category = category[finalI1] + "/" + second_choose[finalI1][finalJ] + "/" + third_choose[finalJ][finalK];
                                if (this_category.equals(first_category)) {
                                    detail_third_item_name.setTextColor(Color.rgb(245, 246,251));
                                }
                                detail_third_card_view.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        if (!this_category.equals(first_category)) {
                                            if (second_last_detail_third_text_view != null) {
                                                second_last_detail_third_text_view.setTextColor(Color.BLACK);
                                            }
                                            second_last_detail_third_text_view = detail_third_item_name;
                                            detail_third_item_name.setTextColor(Color.rgb(224, 143, 87));
                                            second_category = category[finalI1] + "/" + second_choose[finalI1][finalJ] + "/" + third_choose[finalJ][finalK];
                                        }
                                    }
                                });
                                second_detail_third_layout.addView(detail_third_view);
                            }
                        }
                    });
                    second_detail_item.addView(detail_view);
                }
            }
            int finalI = i;
            item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    second_last_cardview.setBackgroundColor(Color.rgb(245, 246,251));
                    second_last_text_view.setTextColor(Color.BLACK);
                    item.setBackgroundColor(Color.WHITE);
                    second_last_cardview = item;
                    item_name.setTextColor(Color.rgb(224, 143, 87));
                    second_last_text_view = item_name;
                    second_detail_item.removeAllViews();
                    second_detail_third_layout.removeAllViews();
                    for (int j = 0; j < second_choose[finalI].length; j++) {
                        View detail_view = LayoutInflater.from(view.getContext()).inflate(R.layout.detail_item_view, null);
                        CardView detail_card_view = detail_view.findViewById(R.id.item);
                        detail_card_view.setPadding(0, 25, 0, 25);
                        TextView detail_item_name = detail_view.findViewById(R.id.item_name);
                        detail_item_name.setText(second_choose[finalI][j]);
                        int finalJ = j;
                        String this_category = category[finalI] + "/" + second_choose[finalI][finalJ];
                        if (this_category.equals(first_category)) {
                            detail_item_name.setTextColor(Color.rgb(245, 246,251));
                        }
                        detail_card_view.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (!this_category.equals(first_category)) {
                                    if (second_last_detail_text_view != null) {
                                        second_last_detail_text_view.setTextColor(Color.BLACK);
                                    }
                                    detail_item_name.setTextColor(Color.rgb(224, 143, 87));
                                    second_last_detail_text_view = detail_item_name;
                                    if (finalI == 0) {
                                        second_detail_third_layout.removeAllViews();
                                        for (int k = 0; k < third_choose[finalJ].length; k++) {
                                            View detail_third_view = LayoutInflater.from(view.getContext()).inflate(R.layout.detail_third_item_view, null);
                                            CardView detail_third_card_view = detail_third_view.findViewById(R.id.item);
                                            detail_third_card_view.setPadding(0, 25, 0, 25);
                                            TextView detail_third_item_name = detail_third_view.findViewById(R.id.item_name);
                                            detail_third_item_name.setText(third_choose[finalJ][k]);
                                            int finalK = k;
                                            String this_category = category[finalI] + "/" + second_choose[finalI][finalJ] + "/" + third_choose[finalJ][finalK];
                                            if (this_category.equals(first_category)) {
                                                detail_third_item_name.setTextColor(Color.rgb(245, 246,251));
                                            }
                                            detail_third_card_view.setOnClickListener(new View.OnClickListener() {
                                                @Override
                                                public void onClick(View view) {
                                                    if (!this_category.equals(first_category)) {
                                                        if (second_last_detail_third_text_view != null) {
                                                            second_last_detail_third_text_view.setTextColor(Color.BLACK);
                                                        }
                                                        second_last_detail_third_text_view = detail_third_item_name;
                                                        detail_third_item_name.setTextColor(Color.rgb(224, 143, 87));
                                                        second_category = category[finalI] + "/" + second_choose[finalI][finalJ] + "/" + third_choose[finalJ][finalK];
                                                    }
                                                }
                                            });
                                            second_detail_third_layout.addView(detail_third_view);
                                        }
                                    }
                                    else {
                                        second_category = category[finalI] + "/" + second_choose[finalI][finalJ];
                                    }
                                }
                            }
                        });
                        second_detail_item.addView(detail_view);
                    }
                }
            });
            second_category_item.addView(item_view);
        }

        second_main_category_well_chosen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Create_Shop_Page.this, Shop_Information_Page.class);
                intent.putExtra("first_category", first_category);
                intent.putExtra("second_category", second_category);
                setResult(3, intent);
                finish();
            }
        });
    }
}